package com.feige.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feige.pojo.Images;
import com.feige.pojo.vo.ImagesQueryVo;
import com.feige.result.AjaxResult;
import com.feige.service.ImagesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 图片表 前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-09-27
 */
@Api(tags = "查询图片表数据")
@RestController
@RequestMapping("/fdfs/images")
public class ImagesController {
    @Resource
    private ImagesService imagesService;
    
    @ApiOperation(value = "获取图片分页列表")
    @PostMapping("/list")
    public AjaxResult index(@RequestBody ImagesQueryVo imagesQueryVo){
        Page<Images> pageParam = new Page<>(imagesQueryVo.getPageNum(), imagesQueryVo.getPageSize());
        QueryWrapper<Images> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(imagesQueryVo.getId())) {
            wrapper.eq("id",imagesQueryVo.getId());
        }
        if(!StringUtils.isEmpty(imagesQueryVo.getModule())) {
            wrapper.like("module",imagesQueryVo.getModule());
        }
        if(!StringUtils.isEmpty(imagesQueryVo.getOriginalFileName())) {
            wrapper.like("original_file_name",imagesQueryVo.getOriginalFileName());
        }
        if(!StringUtils.isEmpty(imagesQueryVo.getOriginalFileName())) {
            wrapper.like("path",imagesQueryVo.getPath());
        }
        if (!StringUtils.isEmpty(imagesQueryVo.getStatus())){
            wrapper.eq("status",imagesQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(imagesQueryVo.getBeginTime())){
            wrapper.ge("gmt_create",imagesQueryVo.getBeginTime());
        }
        if (!StringUtils.isEmpty(imagesQueryVo.getEndTime())){
            wrapper.le("gmt_create",imagesQueryVo.getEndTime());
        }
        imagesService.page(pageParam, wrapper);
        AjaxResult success = AjaxResult.success(pageParam.getRecords());
        success.put("total",pageParam.getTotal());
        return success;
    }


    @ApiOperation(value = "获取图片")
    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable String id) {
        Images images = imagesService.getById(id);
        return AjaxResult.success(images);
    }

    @ApiOperation(value = "修改图片")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Images images){
        boolean update = imagesService.updateById(images);
        if (update){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}

